<template>
  <div>
    <el-dialog :title="user.isAdd?'规格添加':'规格编辑'" :visible.sync="user.isshow" @closed="close">
      {{form}}--{{arr}}
      <el-form :model="form">
        <!-- 2.绑定数据 -->

        <el-form-item label="规格名称" label-width="120px">
          <el-input v-model="form.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="规格属性" label-width="120px" v-for="(item,index) in arr" :key="index">
          <div class="box">
            <el-input v-model="item.value" autocomplete="off"></el-input>
            <el-button type="primary" v-if="index==0" @click="addArr">新增规格属性</el-button>
            <el-button type="danger" v-else @click="delArr(index)">删除</el-button>
          </div>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="user.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="edit">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { indexRouters } from "../../../router";
import { mapGetters, mapActions } from "vuex";
import {
  reqspecsadd,
  reqcateedit,
  reqspecsinfo,
  reqspecsedit,
} from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["user"],
  mounted() {
    this.reqCount();
  },
  data() {
    return {
      form: {
        specsname: "",
        attrs: "",
        status: 2,
      },
      arr: [
        {
          value: "",
        },
        // {
        //   value: "",
        // },
      ],
    };
  },
  computed: {
    ...mapGetters({
      list: "shopSpecs/list",
    }),
  },
  methods: {
    ...mapActions({
      reqList: "shopSpecs/reqLists",
      reqCount: "shopSpecs/reqCount",
    }),
    addArr() {
      this.arr.push({
        value: "",
      });
    },
    delArr(index) {
      this.arr.splice(index, 1);
    },

    //   关闭是否清空form
    close() {
      if (!this.user.isAdd) {
        this.empty();
      }
    },

    //修改
    edit() {
      console.log(this.form);
      this.form.attrs = this.arr.map((item) => item.value).join(",");
      reqspecsedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          //   取消form
          this.cancel();

          // 修改后清空form
          this.empty();
          //   重新刷新数据
          this.reqList();
        }
      });
    },

    //获取一条数据
    getOne(id) {
      reqspecsinfo({
        id: id,
      }).then((res) => {
        if (res.data.code == 200) {
          console.log(res);
          this.form = res.data.list[0];
          // "attrs": [ "S", "M", "L" ] --> [{value:"S"},{value:"M"},{value:"L"}]
          // this.arr = this.form.attrs.map((item) => ({ value: item }));
          this.arr = this.form.attrs.map((item) => ({ value: item }));
        }
      });
    },

    //   取消后form消失
    cancel() {
      this.user.isshow = false;
    },

    // 点击添加后
    add() {
      // arr=[ { "value": "s" }, { "value": "m" }, { "value": "l" }, { "value": "xl" } ]----["s","m","l","xl"]->"s,m,l,xl"
      this.form.attrs = this.arr.map((item) => item.value).join(",");
      // 数据交互
      reqspecsadd(this.form).then((res) => {
        if (res.data.code == 200) {
          //成功了
          // （1）弹成功
          successAlert(res.data.msg);
          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // 请总数
          this.reqCount();
          //   刷新列表
          this.reqList();
        }
      });
    },

    empty() {
      this.form = {
        specsname: "",
        attrs: "",
        status: 2,
      };
      this.arr = [
        {
          value: "",
        },
      ];
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../../less/index.less";
.box {
  display: flex;
}
.box .el-input {
  flex: 1;
}
.box .el-button {
  width: auto;
}
.my-upload {
  width: 100px;
  height: 100px;
  border: 1px dashed @header-color;
  position: relative;
}
.my-plus {
  width: 100%;
  height: 100%;
  text-align: center;
  line-height: 100px;
  color: @parmary;
}
.my-img {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
}
.my-upload input {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
}
</style>